/**
 * webpack中module、plugins等核心配置
 * @created by wjxu on 2017-07-09
 */
const webpack = require('webpack');
const _ = require('lodash');
const path = require('path');
// 处理样式的组件
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const multi = require('./multi.conf.js');
// 遍历得到入口文件
const entries = {};
for (const [key, val] of Object.entries(multi)) {
  if (val.entry) {
    entries[key] = path.join(__dirname, '../web/pages', val.entry);
  }
}
// webpack核心配置
const modules = {
  rules: [{
    test: /\.(js|jsx)$/,
    loader: 'babel-loader',
    options: {
      presets: [
        'react',
        ['env', {
          targets: {
            browsers: ['>0.25%'],
          },
        }],
        'stage-0'],
      plugins: ['transform-runtime', 'transform-decorators-legacy'],
      cacheDirectory: true,
    },
    include: [
      // 只去解析运行目录下的 web 文件夹
      path.join(process.cwd(), './web'),
    ],
    /* exclude: function(path) {
      // 路径中含有 node_modules 的就不去解析。
      var isNpmModule = !!path.match('node_modules/');
      return isNpmModule;
    } */
  }, {
    // loader: 'style!css?sourceMap!sass?sourceMap!import-glob'
    test: /\.css$/i,
    loader: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: [{
        loader: 'css-loader',
      }],
    }),
  }, {
    // loader: 'style!css?sourceMap!sass?sourceMap!import-glob'
    test: /\.less$/i,
    loader: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: [{
        loader: 'css-loader',
      }, {
        loader: 'less-loader',
      }],
    }),
  }],
};
const resolve = {
  modules: ['node_modules'],
  extensions: ['.js', 'jsx', '.less'],
  alias: {
    React: 'react',
    ReactDOM: 'react-dom',
  },
};

/* const _splitChunks = {
  cacheGroups: {
    commons: {
      test: /[\\/]node_modules[\\/]/,
      name: 'vendors',
      chunks: 'all'
    }
  }
}; */

const devLoaders = _.clone(modules.rules);
const prodLoaders = _.clone(modules.rules);
// 处理图片、字体等文件
devLoaders.push({
  test: /\.(png|jpg|gif|eot|woff|woff2|ttf|svg|otf)$/,
  loader: 'file-loader',
  options: {
    name: 'images/[name].[ext]',
    publicPath: '/',
  },
});
prodLoaders.push({
  test: /\.(png|jpg|gif|eot|woff|woff2|ttf|svg|otf)$/,
  loader: 'file-loader',
  options: {
    name: 'images/[name].[hash:5].[ext]',
    publicPath: '/',
  },
});
const webpackConfig = {
  dev: {
    mode: 'development',
    entry: entries,
    module: {
      rules: devLoaders,
    },
    resolve,
    // devtool: 'cheap-source-map',
    devtool: 'inline-source-map',
    // splitChunks: _splitChunks
  },
  prod: {
    mode: 'production',
    entry: entries,
    module: {
      rules: prodLoaders,
    },
    resolve,
    // splitChunks: _splitChunks
  },
  basePlugins: [
    /*new webpack.DllReferencePlugin({
      context: path.join(__dirname), // 需要跟之前保持一致，这个用来指导 Webpack 匹配 manifest 中库的路径；
      manifest: require('./manifest.json'),
    }),*/
    new webpack.ProvidePlugin({
      // Vue: 'vue'
      React: 'react',
      ReactDOM: 'react-dom',
      ReactBootstrap: 'react-bootstrap',
    }),
  ],
};

module.exports = webpackConfig;
module.exports.dir = {
  view: path.join(__dirname, '../dist/views'),
  sourcePath: path.join(__dirname, '../web/pages'),
  publicPath: path.join(__dirname, '../dist/public'),
  distPath: path.join(__dirname, '../dist'),
};
